

//APP部署环境设置
var AppCodeEnv = 1;//0=LOCAL;1=SEVER
var AppDevEvn = 1;//0=Prod;1=DEV

//数据配置
var version = "1.02";
var VI = "714a08411a995a07bc5fa247f5f66bc5";

//网络请求配置
// var pageUrl = "http://192.168.0.101:1009";
// var pageApi = "http://192.168.0.101:1009";
// var baseUrl = "http://192.168.0.5:82";
// var baseApi = "http://192.168.0.5:82";
// var loggerUrl = "http://192.168.0.5:4237";

// var pageUrl = "http://192.168.1.3:1009";
// var pageApi = "http://192.168.1.3:1009";
// var baseUrl = "http://192.168.1.5:1004";
// var baseApi = "http://192.168.1.5:1004";
// var loggerUrl = "http://192.168.1.5:4237";

// var pageUrl = "http://192.168.1.5:1009";
// var pageUrl = "http://192.168.0.99:2001";
// var baseUrl = "http://192.168.0.99:2003";
// var pageUrl = "http://192.168.0.44:2001";
// var baseUrl = "http://192.168.0.44:2003";
// var pageUrl = "https://locserv.sano100.com";
// var baseUrl = "http://locservadmin.sano100.com";
var pageUrl = "https://app.shandaw.com";
var baseUrl = "https://www.shandaw.com";
// var pageUrl = "http://192.168.0.99:2001";
// var baseUrl = "http://192.168.0.99:2003";
var pageApi = pageUrl;
var baseApi = baseUrl;
// var loggerUrl = "http://192.168.43.5:4237";

// var pageUrl = "http:///media/sano/F884A78EC219F0C3/www2/bendifuwu/public
// var pageApi = "http://192.168.0.5:1003";
// var baseUrl = "http://192.168.0.5:1004";
// var baseApi = "http://192.168.0.5:1004";
// var loggerUrl = "http://192.168.0.99:4237";
var loggerUrl = "http://192.168.0.99:4237";

//信息输出
var MyAppLogStatus = 3;//1=console,2=web,3=prod


// var baseUrl = "https://qzapp.sano100.com";
// var baseApi = "https://qzapp.sano100.com";

//图片上传设置，需要在页面加载相应JS
var toOSS = 1;//0:上传到服务器，1：上传到OSS
// var ossUrl = 'http://sano100-tjb.oss-cn-beijing.aliyuncs.com';
var ossUrl = 'http://localservices.oss-cn-qingdao.aliyuncs.com';
//常用图片
var picAdd = "/ApiCloud/assets/img/add.png";
var pic404 = "";
var picUserHead = "/ApiCloud/assets/img/logo_300.jpg";
var picBanner = "/ApiCloud/assets/img/banner.jpg";
var picSBanner = "/ApiCloud/assets/img/banner.jpg";
var picFilePath = pageUrl + "/ApiCloud/assets/img/file.png"


var color1 = "red500";
var color2 = "red500";
var color3 = "red500";

//*********基础功能重新封装**************//
function getAssetBaseUrl() {
    if (toOSS == 1) {
        return ossUrl
    }else {
        return baseUrl
    }
}

function picUrl(url) {
    if (url == undefined) {
        return getAssetBaseUrl();
    }else {
        return getAssetBaseUrl() + url;
    }
}


function isFalse(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    return true
}
function isTrue(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == "undefined"){
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    if (param == "null"){
        return false
    }
    if (param == null){
        return false
    }
    return true
}

function logHtml(info) {
    apiLog(info)
}

var log  = function (info,text){
    if (MyAppLogStatus == 3) {
        return false;
    }
    if (text !== undefined) {
        info = text + info;
    }

    if(MyAppLogStatus == 1){
        console.log(info);
    }
    if(MyAppLogStatus == 2){
        logHtml(
            "==========================================" + "<br>" + "caller:" + log.caller+ "<br>"
            + "--------------------------------------------------------------------------" + "<br>"
            + info + "<br>"
            + "==========================================" + "<br><" + this.caller + "br><br><br>"
            // + "<br><br><br><br>"
        );
    }
    var data = getLocData("htmlLog");
    if(data.open == true){

        logHtml(
            "=========================================="+"<br>"
            + "--------------------------------------------------------------------------" + "<br>"
            + JSON.stringify(info) + "<br>"
            + "=========================================="
            // + "<br><br><br><br>"
        );
    }


}
var logJson = function(info,text){
    info = JSON.stringify(info)
    if (text !== undefined) {
        info = text + "<br/>" + info
    }
    if(MyAppLogStatus == 1){
        console.log(info);
    }
    if(MyAppLogStatus == 2){
        logHtml(
            "==========================================" + "<br>" + "caller:" + logJson.caller + "<br>"
            + "------------------------------------------------------------------------" + "<br>"
            + info + "<br>"
            + "==========================================" + "<br><" + this.caller + "br><br><br>"
        );
    }
    var data = getLocData("htmlLog");
    if(data.open == true){

        logHtml(
            "=========================================="+"<br>"
            + "--------------------------------------------------------------------------" + "<br>"
            + info + "<br>"
            + "=========================================="
            // + "<br><br><br><br>"
        );
    }
}

//打印对象
function dump(obj, index) {
  // appcan.logs(typeof obj)
  if (typeof obj == "object") {
    for (var i in obj) {
      dump(obj[i], i)
    }
  } else {
      console.log(index + ":" + obj + "(" + typeof obj + ")")
  }
}

//URL中的参数转对象
function getDataFromUrl(data){
    var queryArr = data.split("?");
    return queryArr[1];
}
function getDataFromQuery(data){
    var result = {};
    var queryArr = data.split("&");
    queryArr.forEach(function(item){
        var value = item.split("=")[1];
        var key = item.split("=")[0];
        result[key] = value;
    });
    return result;
}

//存储本地信息
function setLocData(key,value){
    delLocData(key)
    if(typeof value != "string"){
        value = JSON.stringify(value);
    }
    api.setPrefs({
        key: key,
        value: value
    });
    // appcan.locStorage.setVal(key,value)
}

//获取本地数据
function getLocData(key){
    //同步返回结果：
    var data = api.getPrefs({
        sync: true,
        key: key
    });
    if( data !== ""){
        try {
            var value = JSON.parse(data);
        }catch (e) {
            value = ""
            return data
        }finally {

        }
        return value
    }
    return data;
}

//删除本地数据
function delLocData(key){
    api.removePrefs({
        key: key
    });
}

//删除所有本地数据
function delAllLocData(){
    api.clearCache(function() {
        api.toast({
            msg: '清除完成'
        });
    });
}

//关于数据传递
function waitFor(targetWindowName,func)
{
    log("iam waitFor")
    appcan.window.subscribe("WaitFor" + targetWindowName , function (msg) {
        try{
            var data = JSON.parse(msg)
        }catch (e){
            showToast("订阅信息类型错误")
        }finally{
        }
        func(data)
    })
}
function callOn(windowName,data)
{
    log("iam callOn")
    if (typeof data != "string") {
        data = JSON.stringify(data)
    }
    appcan.window.publish("WaitFor" + windowName ,data)
}






//点击事件
function ontap(seletor,func){
    appcan.button(seletor,"ani-act",func);
}

function mkSelect (selector,data,onChange) {
    try{
        var option = "";
        data.map(function (d) {
            option = option + '<option value="' + d.id + '"  >' + d.name + '</option>';
        });
        $(selector + " select").append(option);
        appcan.select(selector,onChange);
    }catch (e){
        showToast("select error")
    }finally{

    }
}

function commDatePicker(_this){
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    uexControl.openDatePicker(y,m,d, function(data) {
        var today = data.year + "-" + data.month + "-" + data.day;
        _this.val(today)
    });
}
function commTimePicker(_this){
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var i = date.getMinutes();
    uexControl.openTimePicker(h,i, function(data) {
        //var today = data.year + "-" + data.month + "-" + data.day;
        var time = data.hour + ":" + data.minute
        _this.val(time)
    });
}

//******************用户相关功能******************
function getUser(){
    var user = getLocData("user");
    try{
        user = JSON.parse(user);
    }catch(e){
        return "";
    }finally{}
    return user;
}

function getUid(){
    var user = getUser();
    return user.id
}

function chkUser(){
    try{
        var uid = getUid();
    }catch(e){
        goto("userLogin",{});
        return;
    }finally{}
    if(uid == undefined){
        goto("userLogin",{});
        return;
    }
    return uid;
}

//**********************图片展示相关功能*********************
function mkPicStyle4BackGround(pic){
    return "background-image: url('" + pic + "')"
}



function objDelIndex(obj, index) {
    var tmp = {};
    for (var i in obj) {
        if (i != index) {
            tmp[i] = obj[i]
        }
    }
    return tmp;
}

function objDelValue(obj, value) {
    var tmp = {};
    for (var i in obj) {
        if (obj[i] != value) {
            tmp[i] = obj[i]
        }
    }
    return tmp;
}


function arrayDelIndex(array, index) {
    var tmp = [];
    array.map(function (data, i) {
        if (i != index) {
            tmp.push(data)
        }
    })
    return tmp;
}

function arrayDelValue(array, value) {
    var tmp = [];
    array.map(function (data, i) {
        if (data != value) {
            tmp.push(data)
        }
    })
    return tmp;
}

function isInArray(val ,array) {
    if (!isTrue(array)) {return false}
    for (var i in array) {
        if (array[i] == val) {
            return true;
        }
    }
    return false;
}
function arrayReplaceValue(array, find,replace) {
    var tmp = [];
    array.map(function (data, i) {
        if (data != find) {
            tmp.push(data)
        }else{
            tmp.push(replace)
        }
    })
    return tmp;
}


//这个是用来创建对象用的，暂时不知道为啥
var Class = { // 全局静态类, 用于声明一个新的类并提供构造函数支持
    create: function() {
        return function() { // 返回一个函数, 代表着这个新声明的类的构造函数
            // 一个命名为initialize的函数将被这个类实现作为类的构造函数
            this.initialize.apply(this, arguments);// initialize函数将在你实例化一个变量的时候被调用执行(即上面7个步骤中的第5步)
        }
    }
}

//
// var PageList = new Class.create();
// PageList.prototype = {
//     curPage:0,
//     noMoreData:0,
//     listData:[],
//     listBoxId:"",
//     apiUrl:"",
//     apiParam:{},
//     cbFunc:function(res){},
//     initialize: function(listBoxId , apiUrl ,apiParam) { // 构造函数
//         this.listBoxId = listBoxId
//         this.apiUrl = apiUrl
//         this.apiParam = apiParam
//
//     },
//     getList: function (requestParam) {
//         if (requestParam == undefined) {
//             requestParam = {}
//         }
//         var _this = this;
//         this.apiParam = requestParam
//         requestParam.page = _this.curPage;
//         if (_this.noMoreData == 1) {
//             showToast("没有更多数据了")
//             stopPullDownRefresh()
//             stopPullUpLoadMore()
//             return;
//         }
//         apiGet(_this.apiUrl,requestParam, function (res) {
//             stopPullDownRefresh()
//             stopPullUpLoadMore()
//             var data = res.data
//             var ModelData = {};
//             if (data.length > 0) {
//                 var listData =  _this.listData.concat(res.data)
//                 ModelData[_this.listBoxId] = listData
//                 viewData[_this.listBoxId] = ModelData;
//                 svd(_this.listBoxId)
//                 _this.curPage++
//                 _this.listData = listData
//             }else {
//                 if (_this.curPage == 0) {
//                     ModelData[_this.listBoxId] = []
//                     viewData[_this.listBoxId] = ModelData;
//                     svd(_this.listBoxId)
//                 }
//                 _this.noMoreData = 1
//                 showToast("没有更多数据了")
//             }
//             _this.cbFunc(res)
//         })
//     },
//     reload: function () {
//         this.listData = []
//         this.curPage = 0
//         this.noMoreData = 0
//         this.getList(this.apiParam)
//     }
// }


function eleOffset(ele) {
    return $api.offset($api.dom(ele));
}
function eleWidthAndHeight(ele) {
    var h = $api.dom(ele).offsetHeight;
    var w = $api.dom(ele).offsetWidth;
    return {
        w:w,
        h:h
    }
}
function getWidthAndHeight4ele(ele) {
    var h = $api.dom(ele).offsetHeight;
    var w = $api.dom(ele).offsetWidth;
    return {
        w:w,
        h:h
    }
}

function getWidthAndHeight4Window() {
    var h = api.winHeight ;
    var w = api.winWidth ;
    return {
        w:w,
        h:h
    }
}

function createForm(en) {
    var form = {};
    logJson(en)
    for(var i in en){
        form["form[" + i +"]"] = en[i]
    }
    if (en.id !== undefined && isTrue(en.id)) {
        form["id"] = en.id
    }
    return form;
}
function recreateEn(item) {
    var data= {}
    for (var i in item) {
        var keys = i.split("_")
        var str = ""
        keys.map(function (data, index) {
            if (index !== 0){
                str += data
            }
        })
        data[ str ] = item[i]
    }
    return data
}

function clearEn(en) {
    var data= {}
    for (var i in en) {
        data[ i ] = ""
    }
    return data
}

function getDate(date) {
    if (date == undefined) {
        date = new Date()
    }
    return date.getFullYear() + "-" + ((date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1)) + "-" + date.getDate();
}

function getTime(date) {
    if (date == undefined) {
        date = new Date()
    }
    return date.getHours()
        + ":" + date.getMinutes()
        + ":" + date.getSeconds();
}
function getTimestamp(date) {
    return parseInt(getTimestampMs(date)/1000);
}
function getTimestampMs(date) {
    if (date == undefined) {
        date = new Date()
    }
    return date.getTime();
}

function getDateTime(date) {
    if (date == undefined) {
        date = new Date()
    }
    return date.getFullYear() + "-"
        + ((date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1))
        + "-" + date.getDate()
        + " " + date.getHours()
        + ":" + date.getMinutes()
        + ":" + (date.getSeconds()< 10 ? '0'+date.getSeconds(): date.getSeconds())
}
function timeLeft(str) {
    var date = new Date();
    var now = date.getTime();
//设置截止时间
//     var str = "2019/10/1 00:00:00";
    var endDate = new Date(str);
    var end = endDate.getTime();
//时间差
    var leftTime = end - now;
//定义变量 d,h,m,s保存倒计时的时间
    var d, h, m, s;
    d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
    h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
    m = Math.floor(leftTime / 1000 / 60 % 60);
    s = Math.floor(leftTime / 1000 % 60);
    if (leftTime > 1) {
        return d + "天"
            + h + "时"
            + m + "分";
        // + s + "秒";
    } else {
        //倒计时结束
        return "已过期";
    }
}


function timePass(str) {
    var date = new Date();
    var now = date.getTime();
//设置截止时间
//     var str = "2019/10/1 00:00:00";
    var endDate = new Date(str);
    var end = endDate.getTime();
//时间差
//     var leftTime = end - now;
    var leftTime = now - end;
//定义变量 d,h,m,s保存倒计时的时间
    var d, h, m, s;
    d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
    h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
    m = Math.floor(leftTime / 1000 / 60 % 60);
    s = Math.floor(leftTime / 1000 % 60);

    var showStr = "";
    // if (s > 0) { showStr = s + "秒" + showStr; }
    // if (m > 0) { showStr = m + "分" + showStr; }
    // if (h > 0) { showStr = h + "时" + showStr; }
    // if (d > 0) { showStr = d + "天" + showStr; }
    // return  showStr;
    if (s > 0) { showStr = s + "秒"; }
    if (m > 0) { showStr = m + "分钟"; }
    if (h > 0) { showStr = h + "小时"; }
    if (d > 0) { showStr = d + "天"; }
    return  showStr;
}


function random_string(){
    var len = 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (var i = 0;i<len;i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function get_suffix(filename) {
    pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}

function checkIsPhone(val) {
    var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/;

    if (!myreg.test(val)) {
        return false;
    } else {
        return true;
    }
}


// JavaScript Document
(function($){
    $.fn.myScroll = function(options){
        //默认配置
        var defaults = {
            speed:40,  //滚动速度,值越大速度越慢
            rowHeight:24 //每行的高度
        };

        var opts = $.extend({}, defaults, options),intId = [];

        function marquee(obj, step){

            obj.find("ul").animate({
                marginTop: '-=1'
            },0,function(){
                var s = Math.abs(parseInt($(this).css("margin-top")));
                if(s >= step){
                    $(this).find("li").slice(0, 1).appendTo($(this));
                    $(this).css("margin-top", 0);
                }
            });
        }

        this.each(function(i){
            var sh = opts["rowHeight"],speed = opts["speed"],_this = $(this);
            intId[i] = setInterval(function(){
                if(_this.find("ul").height()<=_this.height()){
                    clearInterval(intId[i]);
                }else{
                    marquee(_this, sh);
                }
            }, speed);

            _this.hover(function(){
                clearInterval(intId[i]);
            },function(){
                intId[i] = setInterval(function(){
                    if(_this.find("ul").height()<=_this.height()){
                        clearInterval(intId[i]);
                    }else{
                        marquee(_this, sh);
                    }
                }, speed);
            });

        });

    }

})(jQuery);
